﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace FuncWorks.Xna.UI.Menu
{
    public class ChoiceItem : MenuItemWithValue
    {
        public int SelectedIndex { get; set; }
        public bool Wrap { get; set; }
		public StringCollection Options { get; private set; }

		public ChoiceItem(string title, TextAlignment alignment, IEnumerable<string> options, int startIndex, bool wrap)
			: base(title, alignment)
        {
			Options = new StringCollection();
			foreach (string o in options) { Options.Add(o); }
			SelectedIndex = (int)MathHelper.Clamp(startIndex, 0, Options.Count - 1);
            Wrap = wrap;
        }

        protected override string DisplayedValue
        {
            get { return Options[SelectedIndex]; }
        }

        public override void Activate()
        {
            SelectedIndex++;
            ApplyLimits();
        }

        public override void MoveLeft()
        {
            SelectedIndex--;
            ApplyLimits();
        }

        public override void MoveRight()
        {
            SelectedIndex++;
            ApplyLimits();
        }

        protected override void ApplyLimits()
        {
            if (SelectedIndex < 0) { SelectedIndex = Wrap ? Options.Count - 1 : 0; }
            if (SelectedIndex >= Options.Count) { SelectedIndex = Wrap ? 0 : Options.Count - 1; }
        }
    }
}
